package com.fcscs.cservice.dao.hibernate;

import com.fcscs.core.dao.ConnectionManager;
import com.fcscs.core.domain.BaseEntity;
import com.fcscs.core.domain.BaseEntity.DBLoc;

public abstract class BaseDaoHibernate<E extends BaseEntity> {

	protected static final String DEL = " set active = false where id = :id ";
	protected static final String UPD = "update ";
	protected static final String ACTIVE = " and active = true ";

	public E save(E entity) {
		getConnectionManager().getCurrentSession(getDBLoc()).saveOrUpdate(entity);
		return entity;
	}

	public void permDelete(E s) {
		getConnectionManager().getCurrentSession(getDBLoc()).delete(s);		
	}

	@SuppressWarnings("unchecked")
	public E get(long id) {
		return (E) getConnectionManager().getCurrentSession(getDBLoc()).load(getCls(), id);
	}

	public Boolean delete(E del) {
		return getConnectionManager().getCurrentSession(getDBLoc()).createQuery(
				UPD + getCls().getSimpleName() +DEL)
		.setLong("id", del.getId()).executeUpdate() == 1;
	}

	protected abstract Class<E> getCls();
	protected abstract DBLoc getDBLoc();
	protected abstract ConnectionManager getConnectionManager();
}
